package com.baidu.cmsWeb.contorllers;

import com.baidu.cmsCommes.pojo.UserDto;
import com.baidu.cmsCommes.pojo.*;
import com.baidu.cmsCommes.util.PageUtil;
import com.baidu.cmsService.services.GroupService;
import com.baidu.cmsService.services.RoleService;
import com.baidu.cmsService.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/17/20:11
 */
@Controller
@RequestMapping
public class AdminContorller {

    @Autowired
    private UserService userService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private RoleService roleService;

    @GetMapping("/index")
    public String index(HttpServletRequest request){
        User user = new User(3, new Date(2013,06,03,13,39,13), "ddddddd@qq.com", "超级管理员", "2bff8bbd20a7006570a02ddeef447e64", "123", 1, "admin");
        request.getSession().setAttribute("user",user);
        request.setAttribute("isAdmin",true);
        return "admin/index";
    }

    @GetMapping("/admin/user/users")
    public String userList(Model model){
        PageUtil.initPage();
        Pager<User> pager = userService.userList();
        model.addAttribute("datas",pager);
        return "user/list";
    }

    @GetMapping("/admin/user/updateStatus/{id}")
    public String updateStatus(@PathVariable("id") Integer id){
        Boolean flag = userService.updateStatusById(id);
        if (flag) {
            return "redirect:/admin/user/users";
        }
        else
            return "error";
    }

    @GetMapping("/admin/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        Boolean flag = userService.deleteUser(id);
        if (flag) {
            return "redirect:/admin/user/users";
        }
        else
            return "error";
    }

    @GetMapping("/admin/user/update/{id}")
    public String selectUserByUpdate(@PathVariable("id") Integer id,Model model){
        UserDto userDto = userService.selectUserById(id);

        List<Group> groups = groupService.groupList();
        List<Role> roles = roleService.roleList();

        if (userDto==null){
            return "error";
        }else {
            //model.addAttribute("userDto", userDto).addAttribute("roles",userDto.getRoleIds()).addAttribute("groups",userDto.getGroupIds());
            model.addAttribute("userDto", userDto).addAttribute("roles",roles).addAttribute("groups",groups);
            return "user/update";
        }
    }

    @PostMapping("/admin/user/update/{id}")
    public String updateUser(UserDto userDto){
        Boolean flag = userService.updateUser(userDto);

        if (!flag)
            return "error";

        return "redirect:/admin/user/users";
    }


    @GetMapping("/admin/user/add")
    public String toSaveUser(Model model){
        List<Group> groups = groupService.groupList();
        List<Role> roles = roleService.roleList();

        if (groups==null || roles==null){
            return "error";
        }
        Model userDto = model.addAttribute("userDto", new UserDto());
        userDto.addAttribute("roles", roles);
        userDto.addAttribute("groups", groups);
        return "user/add";
    }

    @PostMapping("/admin/user/add")
    public String saveUser(UserDto userDto,String confirmPwd){
        if (!userDto.getPassword().equals(confirmPwd))
            return "error";

        Boolean flag = userService.saveUser(userDto);

        if (!flag)
            return "error";

        return "redirect:/admin/user/users";
    }

    @GetMapping("/admin/user/listChannels/{id}")
    public String listChannels(@PathVariable("id") Integer id,Model model){
        System.out.println(id);
        model.addAttribute(userService.selectUserById(id));

        Boolean flag = userService.isAdmin(id);

        if (flag==null)
            return "error";

        if (flag){
            model.addAttribute("isAdmin",1);
        }

        return "user/listChannel";
    }

    @PostMapping("/admin/user/userTree/{uid}")
    @ResponseBody
    public List<Channel> showTree(@PathVariable("uid") Integer id,Model model,Boolean isAdmin){
        List<Channel> channelList = userService.listChannels(id);

        if (channelList== null || channelList.size()==0)
            return null;

        model.addAttribute(channelList);

        return channelList;
    }


}
